<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>异步状态</title>
  </head>
  <body>
    <div id="app">
      <div v-if="error">{{error}}</div>
      <ul v-else-if="list">
        <li v-for="item of list" :key="item.proid">{{item.proname}}</li>
      </ul>
      <div v-else>正在加载....</div>
    </div>
  </body>
  <script src="../lib/vue.global.js"></script>
  <script>
    const { ref, onMounted } = Vue;

    Vue.createApp({
      setup() {
        const list = ref([]);

        const error = ref(null);

        onMounted(() => {
          // ajax 请求
          // fetch 请求基于原生js promise
          // fetch 默认只有get请求  fetch(url, { method: 'post'})
          // fetch 使用最繁琐的地方在于 头信息中 content-type 属性值
          // fetch(url).then(res => res.json()) fetch请求数据需要转换为 json格式
          // fetch(url).then(res => res.json()).then(res => {}).catch(err => {})
          // http://121.89.205.189:3000/apidoc/#api-Pro-GetProList
          // axios  axios.get()  axios.post()
          fetch("http://121.89.205.189:3000/api/pro/list")
            .then((res) => res.json())
            .then((res) => {
              console.log(res);
              list.value = res.data;
            })
            .catch((err) => {
              error.value = err;
            });
        });

        return {
          list,
          error,
        };
      },
    }).mount("#app");
  </script>
</html>
